import Vue from "vue";
import Router from "vue-router";
import Home from "./views/Home.vue";

Vue.use(Router);
const router = new Router({
  mode: "hash",
  base: process.env.BASE_URL,
  routes: [{
      path: "/login",
      component: () => import("./views/login.vue")
    }, {
      path: "/",
      component: Home,
    },
    {
      path: '/home',
      component: Home,
      redirect: '/home/HelloWorld',
      children: [{
          path: '/home/HelloWorld',
          component: () => import('./components/HelloWorld.vue')
        },
        {
          // 用户列表组件
          path: '/home/users',
          component: () => import('./components/users/users.vue')
        }, {
          // 角色列表组件
          path: '/home/roles',
          component: () => import('./components/roles/roles.vue')
        },
        {
          // 权限列表组件
          path: '/home/rights',
          component: () => import('./components/rights/rights.vue')
        }, {
          // 商品列表组件
          path: '/home/goods',
          component: () => import('./components/goods/goods.vue')
        },
        {
          // 添加商品组件
          path: '/home/goodsAdd',
          component: () => import('./components/goodsAdd/goodsAdd.vue')
        },
        {
          // 商品参数
          path: '/home/params',
          component: () => import('./components/params/params.vue')
        },
        {
          // 商品分类组件
          path: '/home/categories',
          component: () => import('./components/categories/categories.vue')
        }, {
          // 订单列表组件
          path: '/home/orders',
          component: () => import('./components/orders/orders.vue')
        }
      ]
    }
  ]
});
// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next();
  let token = window.sessionStorage.getItem('token');
  if (!token) return next('/login');
  next();
})
export default router